<?php

namespace App\Admin\Controllers;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use App\Models\Article;
use App\Models\Category;
use App\Models\Tag;
use Encore\Admin\Grid;
use Encore\Admin\Show;






use function GuzzleHttp\Promise\all;

class ArticleController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '文章管理';


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Article);

        $grid->column('id', __('ID'))->sortable();
        $grid->column('title', __('文章标题'));
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('修改时间'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Article::findOrFail($id));

        $show->field('id', __('ID'));
        $show->field('title', __('文章标题'));
        $show->field('description', __('文章简介'));
        $show->field('content', __('文章内容'));
        $show->tags('标签', function ($tags) {
            $tags->resource('/admin/tags');
            $tags->id();
            $tags->name()->limit(10);
        });
        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('修改时间'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Article);

        $form->display('id', __('ID'));
        $form->text('title', __('文章标题'))->rules('required');
        $form->textarea('description', '文章概述')->rules('required');
        // $form->textarea('content', '文章内容')->rules('required');
        $form->ckeditor('content', '文章内容')->rules('required');

        $form->select('category_id', '分类')->options(Category::all()->pluck('name', 'id'));
        $form->multipleSelect('tags', '标签')->options(Tag::all()->pluck('name', 'id'));

        $form->datetime('created_at', '创建时间');
        $form->datetime('updated_at', '修改时间');

        // $form->display('created_at', __('创建时间'));
        // $form->display('updated_at', __('修改时间'));

        $form->footer(function ($footer) {
            // 去掉`重置`按钮
            $footer->disableReset();
            // // 去掉`提交`按钮
            // $footer->disableSubmit();
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
        });

        return $form;
    }
}
